<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest as LaravelFormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

class FormRequest extends LaravelFormRequest
{
    public function authorize()
    {
    	// Using policy for Authorization
        return true;
    }

    // /**
    //  * 配置验证器实例。
    //  *
    //  * @param  \Illuminate\Validation\Validator  $validator
    //  * @return void
    //  */

    // protected function formatValidationErrors(Validator $validator)
    // {
    //     die();
    //     $message = $validator->errors()->first();
    //     return ['message'=>$message, 'status_code' => 500];
    // }


    // protected function formatErrors(Validator $validator)
    // {
    //     return response(232);
    //     // return $validator->errors()->all();
    // }

    /**
     * Handle a failed validation attempt.
     *
     * @param  \Illuminate\Contracts\Validation\Validator  $validator
     * @return void
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    protected function failedValidation(Validator $validator)
    {
        if($validator->fails()) {
            throw new HttpResponseException(response()->json([
                'status' => 'error',
                'code' => 20000,
                'message' => $validator->errors()->first()
            ], 200));
        }

    }

}